import 'package:enterprise_v6_app/app/core/http/http.dart';
import 'package:enterprise_v6_app/app/core/utils/interface.dart';
import 'package:enterprise_v6_app/app/data/api/special_list.dart';
import 'package:enterprise_v6_app/app/data/entities/application/execute_entity.dart';
import 'package:enterprise_v6_app/app/modules/application/special_operation/special_list/job_ticket/common/common_work_all/common_work_all_controller.dart';
import 'package:get/get.dart';

class CommonSecurityMeasuresController extends GetxController {
  //作业总页面controller
  final workAllPageController = Get.find<CommonWorkAllController>();

  //作业票id
  late String ticketReceiptId;

  //气体检测计时
  int milliseconds = 0;

  //安全措施数据
  List<ExecuteWorkSafetyMeasuresList> safetyMeasuresList = [];

  @override
  void onInit() {
    super.onInit();
    // 初始化页面列表
    initData();
    // 获取数据气体检测有效期
    if (workAllPageController.receiptType == 1) {
      getTicketReceiptValidDate();
    }
  }

  ///初始化数据
  initData() async {
    //作业票id
    ticketReceiptId = workAllPageController.executeEntity.ticketReceiptId!;
    //获取数据-安全措施数据
    final res = await HttpUtil().generateGetRequest<List<ExecuteWorkSafetyMeasuresList>>(
        Interface.getListConfigByTicketReceiptId,
        queryParameters: {'ticketReceiptId': ticketReceiptId}).execute();
    res.onSuccess((result) {
      workAllPageController.executeEntity.workSafetyMeasuresList = result.data;
    });
    //赋值
    safetyMeasuresList = workAllPageController.executeEntity.workSafetyMeasuresList!;
    //给每一项添加作业票id
    for (var item in safetyMeasuresList) {
      item.ticketReceiptId = ticketReceiptId;
    }
    update();
  }

  ///获取数据气体检测有效期
  getTicketReceiptValidDate() async {
    final res = await SpecialListApis.getListConfigByTicketReceiptId(ticketReceiptId).execute();
    res.onSuccess((result) {
      if (result.data != null && result.data!.gasDetectionValidDate != 0) {
        milliseconds = result.data!.gasDetectionValidDate!;
        update();
      }
    });
  }
}
